package com.isoft.demo01.service.impl;



import com.isoft.demo01.dao.UserDao;
import com.isoft.demo01.entity.User;
import io.netty.util.internal.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Service
public class UserService {
    @Autowired
    UserDao userDao;
    public boolean add (String username,String password){
        if(StringUtil.isNullOrEmpty(username)) {
            return false ;
        }
        return userDao.add(username,password) > 0;
    }


    public Map<String,Object> getUser(String username, String password){
        Map<String,Object> map = new HashMap<>();
        String key1 = "result" ;
        String key2 = "data" ;
        if(StringUtil.isNullOrEmpty(username) || StringUtil.isNullOrEmpty(password)) {
            map.put(key1 , 1) ;
            return map ;
        }
        // 按照名字检查账号是否存在
        User d = userDao.chackName(username);
        if (null == d){
            map.put(key1,1);
            return map;
        }
        // 账号检测
        d = userDao.getByAccount(username,password);
        if(null == d){
            map.put(key1, 2);
        }else {
            d.setPassword(null);
            map.put(key1 , 0);
            map.put(key2 , d);
        }
        return map ;

    }
    public boolean adduser (String username,String password,String email,String phone){
        if(StringUtil.isNullOrEmpty(username)) {
            return false ;
        }
        User user=new User();
        user.setBirthday(new Date());
        return userDao.adduser(username,password,email,phone) > 0;
    }
}
